//
//  DataBaseHandle.m
//  FoodFamily
//
//  Created by dlios on 15-6-27.
//  Copyright (c) 2015年 guoBang. All rights reserved.
//

#import "DataBaseHandle.h"

@implementation DataBaseHandle

/*
 


 
 
 self.cookingMethodlable.text = movie.cookingMethod;
 self.fittingCrowdlable.text = movie.fittingCrowd;
;*/
+(instancetype)shareDataBaseHandle
{
    static DataBaseHandle *dataBaseHandle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataBaseHandle = [[DataBaseHandle alloc]init];
        dataBaseHandle.panduan = NO;
        
    });

    return dataBaseHandle;
}
static  sqlite3 *db = nil;

#pragma 打开数据库;
-(void)openDB
{
    if (db !=nil) {
        return;
    }
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [array lastObject];
    NSString *sqlitePath = [filePath stringByAppendingPathComponent:@"dataBase.sqlite"];
    NSLog(@"/*-/*-11111%@",sqlitePath);
    int result = sqlite3_open(sqlitePath.UTF8String, &db);
    if (result ==SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }

    NSLog(@"888888888888888");
}
#pragma 创建表
-(void)createTable
{/*@property(nonatomic,copy)NSString *vegetable_id ;
*/
    NSString *Sql = @"CREATE TABLE IF NOT EXISTS shoucang(number INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,imagePathLandscape TEXT,englishName TEXT,cookingWay TEXT,timeLength TEXT,taste TEXT,effect TEXT,cookingMethod TEXT,fittingCrowd TEXT,vegetable_id TEXT)";
    //title 标题
    int result = sqlite3_exec(db, Sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    
    }


}
#pragma 插入数据
-(void)insertMovie:(Movie *)mov
{
    /* self.englishNamelable.text = movie.englishName;
     self.namelable.text = movie.name;
     self.cookingway.text = movie.cookingWay;
     
     self.timelength.text = movie.timeLength;
     self.tastelable.text = movie.taste;
     self.effectlable.text = movie.effect;
     
     self.cookingMethodlable.text = movie.cookingMethod;
     self.fittingCrowdlable.text = movie.fittingCrowd;
     NSURL *url = [NSURL URLWithString:movie.imagePathLandscape];
     [self.myimageview sd_setImageWithURL:url];
*/

    NSString *sqlstring = [NSString stringWithFormat:@"INSERT INTO shoucang(title,imagePathLandscape,englishName,cookingWay,timeLength,effect,cookingMethod,fittingCrowd,taste,vegetable_id)VALUES('%@' ,'%@','%@','%@','%@','%@','%@','%@','%@','%@')",mov.name,mov.imagePathLandscape,mov.englishName,mov.cookingWay,mov.timeLength,mov.taste,mov.effect,mov.cookingMethod,mov.fittingCrowd,mov.vegetable_id];
    NSLog(@"sqlstring ===%@",sqlstring);
    int result = sqlite3_exec(db, sqlstring.UTF8String, NULL, NULL, NULL);
    if (result==SQLITE_OK) {
        NSLog(@"插入成功");
    }else
    {
        NSLog(@"插入失败");
    
    }


}
#pragma 删除
-(void)deleteTable:(NSString *)name
{//title
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM shoucang WHERE title = '%@'",name];
    NSLog(@"sqlsql=%@",sql);
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }
}
#pragma 查询
-(NSMutableArray *)selectAllMovie
{
NSString *sqlstring = @"SELECT * FROM shoucang";
    sqlite3_stmt *stmt = nil;
    NSMutableArray *array = [NSMutableArray array];
    int reutle = sqlite3_prepare_v2(db, sqlstring.UTF8String, -1, &stmt, nil);
    if (reutle ==SQLITE_OK) {
        NSLog(@"准备成功");
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            const unsigned char *imagePathLandscape = sqlite3_column_text(stmt, 2);
            const unsigned char *englishName = sqlite3_column_text(stmt, 3);
            
            const unsigned char *cookingWay = sqlite3_column_text(stmt, 4);
            const unsigned char *timeLength =sqlite3_column_text(stmt, 5);
            const unsigned char *taste = sqlite3_column_text(stmt, 6);
            
            const unsigned char *effect = sqlite3_column_text(stmt, 7);
            const unsigned char *cookingMethod = sqlite3_column_text(stmt, 8);
            const unsigned char *fittingCrowd = sqlite3_column_text(stmt, 9);
            const unsigned char *vegetable_id = sqlite3_column_text(stmt, 10);
            Movie *movie = [[Movie alloc]init];
            movie.effect = [NSString stringWithUTF8String:(const char *)effect];
            movie.taste = [NSString stringWithUTF8String:(const char *)taste];
            movie.timeLength = [NSString stringWithUTF8String:(const char *)timeLength];
            
            movie.name = [NSString stringWithUTF8String:(const char *)name];
            movie.englishName = [NSString stringWithUTF8String:(const char *)englishName];
            movie.imagePathLandscape = [NSString stringWithUTF8String:(const char *)imagePathLandscape];
            
            movie.cookingWay = [NSString stringWithUTF8String:(const char *)cookingWay];
            movie.cookingMethod = [NSString stringWithUTF8String:(const char *)cookingMethod];
            movie.fittingCrowd = [NSString stringWithUTF8String:(const char *)fittingCrowd];
            movie.vegetable_id = [NSString stringWithUTF8String:(const char *)vegetable_id];
            [array addObject:movie];
        }
        return array;
    }else
    {
    
        NSLog(@"准备失败");
        return array;
    }

}
#pragma 删除表
-(void)dropTable
{
NSString *sqlstring = @"DROP TABLE shoucang";
    int result = sqlite3_exec(db, sqlstring.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
        NSLog(@"删除表成功");
    }else
    {
    
        NSLog(@"删除表失败");
    }


}
@end
